﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class DISKWRITEElement:NmonMulitiCdeElement
    {
        protected new ILog _log = LogManager.GetLogger(typeof(DISKWRITEElement));
        protected SortedDictionary<int, IDataParameter> _zzzz = null;
        protected SortedDictionary<int, IDataParameter> _name = null;
        protected SortedDictionary<int, IDataParameter> _value = null;

        public string Zzzz { get; protected set; }
        public SortedDictionary<int,string> Name { get; protected set; }
        public SortedDictionary<int,float> Value { get; protected set; }



        public DISKWRITEElement(IDbConnection connection)
        {
            Balise = BaliseType.DISKWRITE;

            _zzzz = new SortedDictionary<int, IDataParameter>();
            _name = new SortedDictionary<int, IDataParameter>();
            _value = new SortedDictionary<int, IDataParameter>();
            Name = new SortedDictionary<int, string>();
            Value = new SortedDictionary<int, float>();

            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["DISKWRITE"] + " ([zzzz],[name],[value]) VALUES (@zzzz,@name,@value)";
            

        }
        public DISKWRITEElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (ListCommand == null)
                {
                    ListCommand = new List<System.Data.IDbCommand>();
                    for (int i = 0; i < Name.Count;i++ )
                    {
                        IDbCommand cde = Connection.CreateCommand();
                        cde.CommandText = _insertreq;
                        ListCommand.Add(cde);
                        IDataParameter zzzz = cde.CreateParameter();
                        zzzz.ParameterName = "@zzzz";
                        zzzz.DbType = DbType.String;
                        _zzzz.Add(i,zzzz);
                        IDataParameter name = cde.CreateParameter();
                        name.ParameterName = "@name";
                        name.DbType = DbType.String;
                        _name.Add(i,name);
                        IDataParameter value = cde.CreateParameter();
                        value.ParameterName = "@value";
                        value.DbType = DbType.Decimal;
                        _value.Add(i,value);

                    }
                    
                }
                for(int i=0;i< ListCommand.Count;i++)
                {
                    ListCommand[i].Parameters.Clear();
                    _zzzz[i].Value = Zzzz;
                    _name[i].Value = Name[i];
                    _value[i].Value = Value[i];
                    ListCommand[i].Parameters.Add(_zzzz[i]);
                    ListCommand[i].Parameters.Add(_name[i]);
                    ListCommand[i].Parameters.Add(_value[i]);

                }
                

                


                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("DISKWRITE"))
                {
                    _log.Error("Not DISKWRITE Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Disk"))
                {
                    // information header
                    for (int i = 2; i < elements.Length; i++)
                    {
                        string elemname = elements[i];
                        Name.Add(i-2,elemname);
                    }
                        return false;
                }
                
                Zzzz = elements[1];
                float res = -1;
                for (int i = 2; i < elements.Length; i++)
                {
                    if (float.TryParse(elements[i], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                    {
                        Value[i - 2] = res;

                    }
                    
                }
                
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
